/**
 * Created by xuxujunjunforever on 2015/12/21.
 */
'use strict'
let models = require('../../../modules/models');
let config = require('../../../config');
let roleActions = require('../../../modules/actions/role');
let Fibers = require('fibers');

let RoleDao = models.RoleDao;

class Response {
    json(obj) {
        this.result = obj;
    }
}
let session = {};

function actionCreateRoleNotLogin(test){

    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            roleActions.actionCreateRole(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionCreateRolePrivilegeNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "role":{
                    name: "image_manager_test",
                    privileges: [{id:"567770d02f9b36103d0a8b39"}],
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            roleActions.actionCreateRole(req,res);
        },config.errors.classes.PrivilegeCanNotFound);

        test.done();
    }).run();
}

function actionCreateRoleSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "role":{
                    name: "image_manager_test",
                    privileges: [{id:"5677704c879382401e611e90"}]
                }
            },
            "session": session
        };
        let res = new Response();

        roleActions.actionCreateRole(req, res);
        test.ok(res.result.successful,'creat failed')

        let role = RoleDao.findOne({
            id:res.result.data.role.id
        });
        let issame = true;
        if (role.name != req.body.role.name ) {
            issame = false;
        }
        else{
            for(let i =0;i<role.privileges.length;i++){
                if(role.privileges[i].id!=req.body.role.privileges[i].id){
                    issame = false;
                    break;
                }
            }
        }

        test.ok(issame,'they should be equal');

        test.done();
    }).run();
}

function actionRefreshRoleNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            roleActions.actionRefreshRole(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionRefreshRoleNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "role":{
                    "id":"5667cbd68e731ba021645250"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            roleActions.actionRefreshRole(req,res);
        },config.errors.classes.RoleCanNotFound);

        test.done();
    }).run();
}

function actionRefreshRoleSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "role":{
                    "id":"5667cbd68e731ba02164525c"
                }
            },
            "session": session
        };
        let res = new Response();

        roleActions.actionRefreshRole(req, res);
        test.ok(res.result.successful,'refresh failed')

        let role = RoleDao.findOne({
            id:res.result.data.role.id
        });

        test.deepEqual(role.toObject({recursive:true}),res.result.data.role,'they should be equal');

        test.done();
    }).run();
}

function actionRemoveRoleNotLogin(test){
    new Fibers(function() {
        let req = {"session":{"login": null}
        };
        let res = new Response();

        test.throws(function() {
            roleActions.actionRemoveRole(req,res);
        },config.errors.classes.UserNotLogin);

        test.done();
    }).run();
}

function actionRemoveRoleNotFound(test){
    new Fibers(function() {
        let req = {
            "body":{
                "role":{
                    "id":"5667cbd68e731ba021645250"
                }
            },
            "session":session
        };
        let res = new Response();

        test.throws(function() {
            roleActions.actionRemoveRole(req,res);
        },config.errors.classes.RoleCanNotFound);

        test.done();
    }).run();
}

function actionRemoveRoleSuccessful(test){
    new Fibers(function() {
        let req = {
            "body": {
                "role": {
                    "id":"5677782ce654d8c40e28d0aa",
                }
            },
            "session": session
        };
        let res = new Response();

        roleActions.actionRemoveRole(req, res);
        test.ok(res.result.successful,'remove failed')

        let role = RoleDao.findOne({
            id:req.body.role.id
        });

        test.deepEqual(role,null,'user should not be found');

        test.done();
    }).run();
}


module.exports = {
    setUp: function (callback){
        session = {
            "login":{
                "user": {
                    "id":"5667cbd68e731ba02164525d",
                    "name": "test",
                    "password": "e10adc3949ba59abbe56e057f20f883e"
                }
            }
        };
        callback();
    },
    actionCreateRoleNotLogin,
    actionCreateRolePrivilegeNotFound,
    actionCreateRoleSuccessful,
    actionRefreshRoleNotLogin,
    actionRefreshRoleNotFound,
    actionRefreshRoleSuccessful,
    //actionRemoveRoleNotLogin,
   //actionRemoveRoleNotFound,
    //actionRemoveRoleSuccessful
};

